<?php
namespace app\index\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

class Login extends Controller{

    public function index(){
    	return $this->fetch('Login/index');
    }

    public function userLogin(){
		$param = myarr($this->request->post());
		$name = $param['user'];
		if($name==null) return ['msg' => '账号不能为空'];
        if(preg_match('/(^1[3|4|5|6|7|8|9]\d{9}$)|(^09\d{8}$)/', $name)){
            $name = createCode($name);
        }
//		 return ['msg'=>$name];
		$user = Db::name('users')->where(['phonenumber|idcard|assetaccount'=>$name])->find();
		// echo json_encode($user);
		if($user&&$user['password']==$param['pwd']){
			if($user['statesinfo']==0){
                if($user['lc']!=1) {
                    Session::set('uid', $user['id']);
                    Session::set('names', $user['name']);
                    Session::set('phonenumber', $param['user']);
                    Session::set('codes', mt_rand(1000, 9999));
                    // Session::set('registerID',$user['register']);
                    //获取最后一次登录的ip
                    $ip = Db::name('login_record')->where(['uid' => $user['id']])->order('id', 'desc')->find();
                    if ($ip) {
                        Session::set('lastIP', $ip['logintoip']);
                        Session::set('lasttime', $ip['logintime']);
                        $da = [
                            'uid' => $user['id'],
                            'logintoip' => $_SERVER['REMOTE_ADDR'],
                            'logintime' => date('Y-m-d H:i:s'),
                            'operation' => 'login'
                        ];
                        Db::name('login_record')->insert($da);
                    } else {
                        $da = [
                            'uid' => $user['id'],
                            'logintoip' => $_SERVER['REMOTE_ADDR'],
                            'logintime' => date('Y-m-d H:i:s')
                        ];
                        Db::name('login_record')->insert($da);
                    }
                    if($user['password']!='123456'){
                        if ($user['RNA'] == 0) {
                            return ['msg' => 'success'];
                        } else {
                            return ['msg' => 'success_s'];
                        }
                    }else{
                        return ['msg' => 'success_xg'];
                    }
                }else{
                    return ['msg' => 'success_ss'];
                    //return  "<script>alert('请尽快平仓，升级新系统');window.location.href='http://www.lcqjgj.com/home/Userlogin/loginOut'</script>";
                 }


			}else{
				return ['msg'=>'账户异常'];
			}
		}else{	
			return ['msg'=>'账户或密码错误'];
		}
		
    }

    //退出登录
    public function loginOut(){
        session(null);
        cookie('a_logo',null);
        return $this->fetch('Login/index');
    }

    public function se(){
        var_dump(session(''));
    }

}
